<?php
namespace Home\Model;
use Think\Model;
class OrderModel extends Model {
	public $table='think_order';
	public $page; //当前页
	public $perpage; //每页显示
	public $order_id; //ID
	public $post; //提交的信息 数组
	public $ids; //IDs 主要用于删除
	public $sorts; //IDs 主要用于删除
	public $keyword; //关键词
	public $data; //传递的数组数据

	//获得列表 $page当前页 $perpage每页显示 当前默认用户名 姓名
	public function get_order_list()
	{
		$page=$this->page;
		$perpage=$this->perpage;
		//搜索
		//搜索
		$where='where 1 and ((a.buyer_id=b.member_id and a.buyer_id=d.member_id) or (a.seller_id=b.member_id and a.seller_id=d.member_id)) ';
		if($this->condition){
			//$this->keyword='2016';
			$where.=" and ".$this->condition;
		}
		/*$where='where 1 ';
		if($this->keyword){
			$where.="and `order_sn`  LIKE '%".$this->keyword."%'";
		}*/
		/*
		if($this->pc_id){
			$pc_ids=$this->get_sub_pc_id($this->pc_id);
			$pc_ids=mb_strcut($pc_ids,1); //裁掉第一个，
			$where.="and c.pc_id in(".$pc_ids.")";
		}*/
		$start=($page-1)*$perpage;
		$Model=new Model();
		$Member=M("Member");
		$Company=M("Company");
		//$list=$Model->query("select order_id,order_sn,seller_id,buyer_id,c_id,g_id,order_amount,order_time,order_state_id,cg_type from ".$this->table." ".$where." order by order_id desc limit $start,$perpage");
		$sql="select DISTINCT(a.order_id),a.order_sn,a.seller_id,a.buyer_id,a.c_id,a.g_id,a.order_amount,a.order_time,a.order_state_id,a.cg_type from think_order a,think_member b,think_company d ".$where." order by a.".$this->order." limit $start,$perpage";
		//echo $sql;exit;
		$list=$Model->query($sql);
		//dump($list);exit;
		foreach($list as $k=>$v){
			//货品订单
			if($v['cg_type']==3){
				if($v['c_id']){
					$list[$k]['purchase_title']=M('Purchase')->where('purchase_id='.$v['c_id'])->getField('purchase_title');
				}
				if($v['g_id']){
					$list[$k]['supply_title']=M('Supply')->where('supply_id='.$v['g_id'])->getField('supply_title');
				}
			}
			//采购意向
			if($v['cg_type']==1){
				if($v['c_id']){
					$list[$k]['purchase_title']=M('Purchase_intention')->where('intention_id='.$v['c_id'])->getField('intention_message');
					//$intention_supply_id=M('Purchase_intention')->where('intention_id='.$v['c_id'])->getField('supply_id');
					//$list[$k]['supply_title']=M('Supply')->where('supply_id='.$intention_supply_id)->getField('supply_title');
				}
				if($v['g_id']){
					$list[$k]['supply_title']=M('Supply')->where('supply_id='.$v['g_id'])->getField('supply_title');
				}
			}
			//供应意向
			if($v['cg_type']==2){
				if($v['c_id']){
					$list[$k]['purchase_title']=M('Purchase')->where('purchase_id='.$v['c_id'])->getField('purchase_title');
				}
				if($v['g_id']){
					//$list[$k]['supply_title']=M('Supply')->where('supply_id='.$v['g_id'])->getField('supply_title');
					$list[$k]['supply_title']=M('Supply_intention')->where('intention_id='.$v['g_id'])->getField('intention_message');
				}
			}
			$list[$k]['seller_truename']=$Member->where('member_id='.$v['seller_id'])->getField('member_truename');
			$list[$k]['seller_companyname']=$Company->where('member_id='.$v['seller_id'])->getField('company_name');
			$list[$k]['buyer_truename']=$Member->where('member_id='.$v['buyer_id'])->getField('member_truename');
			$list[$k]['buyer_companyname']=$Company->where('member_id='.$v['buyer_id'])->getField('company_name');
			$list[$k]['order_state_name']=M('Order_state')->where('order_state_id='.$v['order_state_id'])->getField('order_state_name');
		}
		//dump($list);exit;
		return $list;
	}
	//获得总数
	public function get_order_total()
	{
		//搜索
		$where='where 1 and ((a.buyer_id=b.member_id and a.buyer_id=d.member_id) or (a.seller_id=b.member_id and a.seller_id=d.member_id)) ';
		if($this->keyword){
			//$this->keyword='2016';
			$where.=" and (`order_sn` LIKE '%".$this->keyword."%' or `member_email` LIKE '%".$this->keyword."%' or `member_mobile` LIKE '%".$this->keyword."%' or `member_truename` LIKE '%".$this->keyword."%' or `company_name` LIKE '%".$this->keyword."%')";
		}
		/*if($this->pc_id){
			$pc_ids=$this->get_sub_pc_id($this->pc_id);
			$pc_ids=mb_strcut($pc_ids,1); //裁掉第一个，
			$where.="and pc_id in(".$pc_ids.")";
		}*/
		$Model=new Model();
		$sql="select count(DISTINCT(a.order_id)) as total from think_order a,think_member b,think_company d ".$where;
		$query=$Model->query($sql);
		//$list=$Model->query($sql);
		return $query[0]['total'];
	}
	//获取信息 更改为新Order表的 改写，简化
	public function get_order_info()
	{
		$order_id=intval($this->order_id);
		$Model=M("Order");
		//$Member=M("Member");
		//$Company=M("Company");
		//$OrderProduct=M("Order_product");
		//$OrderAddress=M("Order_address");
		$OrderVoucher=M("Order_voucher");
		//$Match=M("Match");
		//$Purchase=M("Purchase");
		//$Supply=M("Supply");
		$info=$Model->where('order_id='.$order_id)->find();
		//dump($info);exit;
		//$info['match']=$Match->where('match_id='.$info['match_id'])->find();
		$info['seller_info']=$this->getMemberInfo($info['seller_id']);  //卖家信息
		$info['buyer_info']=$this->getMemberInfo($info['buyer_id']);    //买家信息
		//获取凭证列表
		$info['supply_voucher']=$OrderVoucher->where('order_id='.$order_id)->getField('supply_voucher');
		$info['supply_voucher_list']=$this->getSupplyVoucherList($order_id);
		$info['purchase_voucher']=$OrderVoucher->where('order_id='.$order_id)->getField('purchase_voucher');
		$info['purchase_voucher_list']=$this->getPurchaseVoucherList($order_id);
		//订单状态列表
		$info['order_state_list']=M('Order_state')->where('order_state_is_show=1')->field('order_state_id,order_state_name')->order('order_state_sort')->select();
		//物流公司 单号
		$info['logistics_company_list']=M('Logistics_company')->field('logistics_company_id,logistics_company_name')->where('logistics_company_show=1')->order('logistics_company_sort asc,logistics_company_id asc')->select();
		$info['order_logistics_info']=$this->getOrderLogistics($order_id);
		//收货地址列表
		$info['member_address_list']=M('Member_address')->where('member_id='.$info['buyer_id'])->field('member_address_id,member_zipcode,member_address,member_truename,member_mobile,member_telephone,is_default')->order('is_default desc')->select();
		//$info['member_address_id']=M('Order_address')->where('order_id='.$order_id)->getField('member_address_id');
		//根据cg_type，查询不同的数据
		if($info['cg_type']==3){
			$info['supply_info']=$this->getSupplyInfo($info['g_id']);  //供应信息
			$info['purchase_info']=$this->getPurchaseInfo($info['c_id']);  //采购信息
		}
		//采购意向
		if($info['cg_type']==1){
			$info['purchase_info']['purchase_title']=strip_tags(M('Purchase_intention')->where('intention_id='.$info['c_id'])->getField('intention_message'));  //采购信息
			$info['supply_info']=$this->getSupplyInfo($info['g_id']);  //供应信息
		}
		//供应意向
		if($info['cg_type']==2){
			$info['purchase_info']=$this->getPurchaseInfo($info['c_id']);  //采购信息
			$info['supply_info']['supply_title']=strip_tags(M('Supply_intention')->where('intention_id='.$info['g_id'])->getField('intention_message'));  //供应信息
		}
		//dump($info);exit;
		return $info;
	}

	//获取订单物流信息
	private function getOrderLogistics($order_id){
		$arr=array();
		$arr['logistics_no']=M('Order_logistics')->where('order_id='.$order_id)->getField('logistics_no');
		$arr['logistics_company_id']=M('Order_logistics')->where('order_id='.$order_id)->getField('logistics_company_id');
		if($arr['logistics_company_id']>0){
			$arr['logistics_company_name']=M('Logistics_company')->where('logistics_company_id='.$arr['logistics_company_id'])->getField('logistics_company_name');
		}
		//dump($arr);exit;
		return $arr;exit;
	}
	//获取订单地址
	private function getOrderAddress($order_id){
		$arr=array();
		$member_address_id=M('order_address')->where('order_id='.$order_id)->getField('member_address_id');
		//echo $member_address_id;exit;
		if($member_address_id>0){
			$arr=M('member_address')->where('member_address_id='.$member_address_id)->field('member_zipcode,member_address,member_truename,member_mobile,member_telephone')->find();
		}
		//dump($arr);exit;
		return $arr;exit;
	}

	//获取会员信息
	private function getMemberInfo($member_id){
		$arr=array();
		$company_info=M('Company')->where('member_id='.$member_id)->field('company_name,company_part,company_position')->find();
		$member_info=M('Member')->where('member_id='.$member_id)->field('member_truename,member_mobile,member_email,member_balance')->find();
		$member_address_list=M('Member_address')->where('member_id='.$member_id)->field('member_address_id,member_zipcode,member_address,member_truename,member_mobile,member_telephone,is_default')->order('is_default desc')->select();
		foreach($company_info as $k=>$v){
			$arr[$k]=$v;
		}
		foreach($member_info as $k=>$v){
			$arr[$k]=$v;
		}
		$arr['member_address_list']=$member_address_list;
		//dump($arr);exit;
		return $arr;exit;
	}

	//查供货相关信息
	private function getSupplyInfo($supply_id){
		$supply_id=addslashes($supply_id);
		$arr=array();
		//$supply_info=M('Supply')->where('supply_id='.$supply_id)->field('supply_title,supply_price,area_id')->find();
		$supply_info=M('Supply')->where('supply_id='.$supply_id)->find();
		//dump($supply_info);exit;

		//dump($company_info);exit;
		foreach($supply_info as $k=>$v){
			$arr[$k]=$v;
		}
		/*foreach($member_info as $k=>$v){
			$arr[$k]=$v;
		}*/
		//dump($arr);exit;
		return $arr;exit;
	}

	//查供货相关信息
	private function getPurchaseInfo($purchase_id){
		$purchase_id=addslashes($purchase_id);
		$arr=array();
		//$supply_info=M('Supply')->where('supply_id='.$supply_id)->field('supply_title,supply_price,area_id')->find();
		$purchase_info=M('Purchase')->where('purchase_id='.$purchase_id)->find();
		//dump($purchase_info);exit;

		//dump($company_info);exit;
		foreach($purchase_info as $k=>$v){
			$arr[$k]=$v;
		}
		/*foreach($member_info as $k=>$v){
			$arr[$k]=$v;
		}*/
		//dump($arr);exit;
		return $arr;exit;
	}

	//获取供货凭证列表
	private function getSupplyVoucherList($order_id){
		$order_id=addslashes($order_id);
		$supply_voucher=M('order_voucher')->where('order_id='.$order_id)->getField('supply_voucher');
		$list=unserialize($supply_voucher);
		//dump($list);exit;
		return $list;
	}
	//获取采购凭证列表
	private function getPurchaseVoucherList($order_id){
		$order_id=addslashes($order_id);
		$purchase_voucher=M('order_voucher')->where('order_id='.$order_id)->getField('purchase_voucher');
		$list=unserialize($purchase_voucher);
		//dump($list);exit;
		return $list;
	}
	//编辑提交 更新与订单相关个表
	public function order_edit_post()
	{
		$Model=M("Order");
		$OrderAddress=M("Order_address");
		$OrderVoucher=M("Order_voucher");
		$OrderLogistics=M("Order_logistics");
		$where['order_id'] = $this->post['order_id'];
		$data=$this->post;
		//dump($this->post);exit;
		$result=$Model->where($where)->save($data);
		$result2=$OrderAddress->where($where)->save($data);
		$result3=$OrderVoucher->where($where)->save($data);
		$result4=$OrderLogistics->where($where)->save($data);
		if($result===false||$result2===false||$result3===false||$result4===false) {
			return false;
		}else{
			return true;
		}
	}
	//删除
	public function delete_order()
	{
		$ids=$this->ids;
		$Model=M("Order");
		foreach($ids as $v){
			$condition['order_id']=$v;
			if(!$Model->where($condition)->delete()){
				return false;
			}
		}
		return true;
	}
	//写入订单 后台创建的，一并写入order相关表
	public function add_order(){
		$Model=M("Order");
		//$Model2=M("Order_address");
		$Model3=M("Order_logistics");
		$Model4=M("Order_voucher");
		$data=$this->data;
		//写入订单表
		$result=$Model->add($data);
		if($result===false){
			return false;
		}
		//继续写入或更新
		$order_id=$Model->where("order_sn='".$data['order_sn']."'")->getField('order_id');
		$data['order_id']=$order_id;
		//$result2=$Model2->add($data);
		$result3=$Model3->add($data);
		$result4=$Model4->add($data);
		if($result3===false||$result4===false){
			return false;
		}
		return true;
	}

	//用于统计中的订单额度数据 最近7日数据
	public function get_day_order_total(){
		//echo date('Y-m-d H:i:s',time());exit;
		$Model=M('Order');
		//$start_time= date('m月d日', strtotime('-7 days'));
		$j=0;
		$today=mktime(0,0,0,date('m'),date('d'),date('Y'));
		//echo date('YmdHis',$today);exit;
		for($i=6;$i>=0;$i--){
			$start_time=$today-$i*24*60*60;
			$end_time=$today-($i-1)*24*60*60;
			/*if($i==0){
				echo date('YmdHis',$start_time).date('YmdHis',$end_time).'<br />';
			}*/
			$data[$j]['label']=date('n月j日', strtotime('-'.$i.' days'));
			$data[$j]['value']=$Model->where("order_amount>1 and order_time>=".$start_time." and order_time<".$end_time)->sum('order_amount');
			//$data[$j]['start']=date('Y-m-d H:i:s', $start_time);
			//$data[$j]['end']=date('Y-m-d H:i:s', $end_time);
			$j++;
		}
		//dump($data);exit;
		return json_encode($data);
	}

	//月统计
	public function get_month_order_total(){
		$Model=M('Order');
		$year = date('Y');
		$max=date('n');
		//$month = (int)date('m');
		for($i=0;$i<$max;$i++){
			$start_time=mktime(0,0,0,($i+1),1,$year);
			$end_time=mktime(0,0,0,($i+2),1,$year);
			$data[$i]['label']=$year.'年/'.($i+1).'月';
			$data[$i]['value']=$Model->where("order_amount>1 and order_time>=".$start_time." and order_time<".$end_time)->sum('order_amount');
		}
		//dump($data);exit;
		return json_encode($data);
	}

	//首页订单月统计 用于给用户展示
	public function get_index_order_total(){
		$Model=M('Order');
		$year = date('Y');
		$max=date('n');
		//$month = (int)date('m');
		for($i=0;$i<$max;$i++){
			$start_time=mktime(0,0,0,($i+1),1,$year);
			$end_time=mktime(0,0,0,($i+2),1,$year);
			$data[$i]['label']=$year.'年/'.($i+1).'月';
			$data[$i]['value']=$Model->where("order_amount>1 and order_time2>=".$start_time." and order_time2<".$end_time)->sum('order_amount');
		}
		//dump($data);exit;
		return json_encode($data);
	}

	//excell导出
	public function get_order_xls(){

		$Model= M("Order");
		$Member=M("Member");
		$Company=M("Company");
		$ProductClass=M("ProductClass");
		$Unit=M("Unit");
		$order_list = Array();
		$list=$Model->Field('order_sn,order_time,pc_id,seller_id,buyer_id,order_num,order_price,unit_id,order_amount')->order("order_id desc")->select();
		foreach($list as $k=>$v){

			$order_list[$k]['order_sn'] = " ".$v["order_sn"];
			$order_list[$k]['order_time'] = date("Y-m-d",$v["order_time"]);
			$order_list[$k]['delivery_time'] = date("Y-m-d",$v["order_time"]);
			if($v["pc_id"]==""){
				$order_list[$k]['pc_name'] = "\\";
			}else{
				$order_list[$k]['pc_name'] = $ProductClass->where('pc_id='.$v['pc_id'])->getField('pc_name');
			}
			$order_list[$k]['seller_name']=$Company->where('member_id='.$v['seller_id'])->getField('company_name').$Member->where('member_id='.$v['seller_id'])->getField('member_truename');
			$order_list[$k]['buyer_name']=$Company->where('member_id='.$v['buyer_id'])->getField('company_name').$Member->where('member_id='.$v['buyer_id'])->getField('member_truename');
			$order_list[$k]['order_num'] = $v["order_num"];
			$order_list[$k]['order_price'] = $v["order_price"];
			$order_list[$k]['order_amount'] = $v["order_amount"];
		}
		//dump($list);exit;
		return $order_list;
	}

}